package com.youtellv1.mapper;

import com.youtellv1.entity.Teachers;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeachersMapper extends BaseMapper<Teachers>{
    @Select("select * from tb_teachers order by tid limit #{offset}, #{limit}")
    List<Teachers> findAllTeachers(@Param("offset") int offset, @Param("limit") int limit);


    @Select("select * from tb_teachers where tid = #{tid}")
    Teachers findTeachersById(Teachers teachers);

    @Select("select * from tb_teachers where tid = #{tid}")
    Teachers findTeaById(@Param("tid") Integer tid);

    @Select("select tname from tb_teachers where tid = #{tid}")
    Teachers findNameByTid(@Param("tid") Integer tid);


    @Insert("insert into tb_teachers(tname, tsex, timg, tage, tyear, tspec, tschool, tintr, ttype, tphone, flag) values (#{tname}, #{tsex}, #{timg}, #{tage}, #{tyear}, #{tspec}, #{tschool}, #{tintr}, #{ttype}, #{tphone}, #{flag})")
    void save(Teachers teachers);

    @Update("update tb_teachers set flag = #{flag} where tid = #{tid}")
    void updateFlag(@Param("tid") Integer tid, @Param("flag") Integer flag);

    @Update("<script>" +
            "update tb_teachers " +
            "<set>" +
            "    <if test='tname != null'>tname = #{tname},</if>" +
            "    <if test='tsex != null'>tsex = #{tsex},</if>" +
            "    <if test='timg != null'>timg = #{timg},</if>" +
            "    <if test='tage != null'>tage = #{tage},</if>" +
            "    <if test='tyear != null'>tyear = #{tyear},</if>" +
            "    <if test='tspec != null'>tspec = #{tspec},</if>" +
            "    <if test='tschool != null'>tschool = #{tschool},</if>" +
            "    <if test='tintr != null'>tintr = #{tintr},</if>" +
            "    <if test='ttype != null'>ttype = #{ttype},</if>" +
            "    <if test='tphone != null'>tphone = #{tphone},</if>" +
            "    <if test='flag != null'>flag = #{flag}</if>" +
            "</set>" +
            "where tid = #{tid}" +
            "</script>")
    void update(Teachers teachers);

    @Delete("delete from tb_teachers where tid = #{tid}")
    void delete(Teachers teachers);

    @Select("select count(*) from tb_teachers where flag = 1")
    Integer teachersCount();

    @Select("SELECT tid, tname, timg, ttype, tyear FROM tb_teachers WHERE tid = #{id}")
    Teachers findById(@Param("id") Integer id);
}
